home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 1620 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  3.2 KB

  1. Path: news.uh.edu!sukku
  2. From: sukku@menudo.uh.edu (sukumar)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: GoTo equivalent in C ??
  5. Date: 15 Jan 1996 17:53:18 GMT
  6. Organization: University of Houston
  7. Message-ID: <4de4ae$h6a@masala.cc.uh.edu>
  8. References: <4d67vm$e5h@masala.cc.uh.edu> <4d8sa6$mqc@news.iag.net>
  9. NNTP-Posting-Host: menudo.uh.edu
  10. X-Newsreader: TIN [version 1.2 PL2]
  11.  
  12. John R Buchan (jatmon@iag.net) wrote:
  13. : In article <4d67vm$e5h@masala.cc.uh.edu>, sukku@menudo.uh.edu says...
  14. : >
  15. : >Hi,
  16. : >        I have always thought about this. How do you get the effect of goto
  17. : >in C without using "goto"?? For ex, if I have to check for an error in my
  18. : >function for every computation I do, and then do some cleaning up, how do 
  19. : >I do it. Here is an example:
  20. : >
  21. : >f()
  22. : >{
  23. : >
  24. : >char *s = (char *) malloc(10 * sizeof(char));
  25. : >int i;
  26. : >
  27. : >i = scanf("%s", s);
  28. : >
  29. : >if(i != 1) /* I wish I could do a goto to the last two lines of the 
  30. : function*/
  31. : >{
  32. : >        free(s); 
  33. : >        return;
  34. : >}
  35. : >
  36. : >i = atoi(s);
  37. : >if(i==0)
  38. : >{
  39. : >        free(s);
  40. : >        return;
  41. : >
  42. : >}
  43. : >free(s);
  44. : >return;
  45. : >
  46. : >}
  47.  
  48. : f()
  49. :    {
  50. :    int i;
  51. :    char *s = malloc(10 * sizeof(char));
  52.  
  53. :    if( s != NULL)                /* allocation succeeeded */
  54. :       {
  55. :       if( (scanf("%s", s)) == 1) /* 1 field input */
  56. :          {
  57. :          if( (i = atoi(s)) != 0) /* converted to non-zero integer */
  58. :             {
  59. :             /* more code */
  60. :             }
  61. :          }
  62. :       free(s);
  63. :       }
  64. :    return;
  65. :    }
  66.  
  67. : or
  68.  
  69. : f()
  70. :    {
  71. :    int i;
  72. :    char *s = malloc(10 * sizeof(char));
  73.  
  74. :    if( s != NULL)                /* successful allocation */
  75. :       {
  76. :       if( ((scanf("%s", s)) == 1)) && ((i = atoi(s)) != 0) ) /* valid input */
  77. :          {
  78. :          /* more code */
  79. :          }
  80. :       free(s);
  81. :       }
  82. :    return;
  83. :    }
  84.  
  85. : Note: The && operator guarantees that the scanf test must succeed _before_
  86. : the atoi can be called.
  87.  
  88. : -- 
  89. : John R Buchan           -:|:-     Looking for that elusive FAQ?  ftp to:
  90. : jatmon@mail.iag.net     -:|:-     rtfm.mit.edu /pub/usenet-by-group/....
  91.  
  92. The case is not as simple as this one always. Typically it is like this:
  93.  
  94.  
  95. (Alloc memory for a bunch of vars)
  96. ...
  97. for every computation, if error flagged, clean up and return
  98.  
  99.  
  100. This could be achieved by using one error flag like this:
  101.  
  102. int flag =0;
  103.  
  104.  
  105. If(computaion fails)
  106.     flag = 0;
  107.  
  108.  
  109. if(!flag)
  110. {
  111.  Do computation
  112. If(computaion fails)
  113.     flag = 0;
  114. }
  115.  
  116.  
  117. ...
  118.  
  119. if(flag)
  120.     Clean up and print error messages and do the relevant stuff and return
  121. else 
  122.     dealloc and return
  123.  
  124.  
  125. This must be a common scenario for all of us. I have been under the impression
  126. that "goto" might make the code a look like a puzzle and I religiously 
  127. followed the rule "Never goto GOTO". This made my code very complex. 
  128. Often it lead to leaks.
  129. I would return in the middle of a function forgetting to cleanup only to be
  130. detected by testcenter. This also results in code redundancy. 
  131.  
  132. What does everyone do?? I have always been tempted to use goto especially in 
  133. functions involving a lot of computation and error checking.
  134. I have heard people say "People who don't know how to code use GOTO". I was
  135. curious to know how guys that say this solve the above problem.
  136.  
  137. Cheers,
  138. Srini.
  139.  
  140.  
  141.  
  142.  
  143.     
  144.